/*
 * TITLE:Login
 * AUTHOR:Manori Thakur
 * PURPOSE: Handles login
 */
package com.example.android.debuggermenu;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.*;
import clientcore.*;


public class Login extends DebuggerActivity {
	private static final String TAG = Login.class.getSimpleName();
    private Context context = this;
    private String username=null,password=null;
    private Button login = null;
    private EditText user, passwd;
    private TextView checkLogin;
    private ProgressDialog progressDialog = null;
    private Socket gameClient;
    private DataOutputStream sendServer;
    private DataInputStream receiveServer;    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        GameState.initialize();
        if(GameState.isConnected()) {
        setContentView(R.layout.login_layout);
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Please wait...");
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(false);

        login = (Button)findViewById(R.id.buttonLogin);
        user = (EditText)findViewById(R.id.username);
        passwd = (EditText)findViewById(R.id.pwd);
        checkLogin = (TextView)findViewById(R.id.checkLogin);

        login.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if(user.getText().length()>0 && passwd.getText().length()>0) {
                   GameState.setUserName(
                   username = user.getText().toString());
                   password = passwd.getText().toString();
                   
                   // Attempt to login. If success, launch game.
                   short status = GameState.login(username, password);
                   if(status == 2){
                       checkLogin.setText("Logged out");
                   }else if(status == 1 || status == 3) {
                       checkLogin.setText("Invalid username-password combination.");
                   }else {
                	   checkLogin.setText(" ");
                       Intent launchGame = new Intent(Login.this, Game.class);
                       startActivity(launchGame);
                   }
                }
        }});
        }
        else {
        	AlertDialog.Builder builder = new AlertDialog.Builder(this);
        	builder.setIcon(R.drawable.bug);
        	builder.setMessage("Unable to connect to server.");
        	builder.setCancelable(false);
        	builder.setPositiveButton("Return to Main Menu", new DialogInterface.OnClickListener() {
        	           public void onClick(DialogInterface dialog, int id) {
        	                ((Activity) context).finish();
        	           }
        	       });
        	AlertDialog alert = builder.create();
        	alert.show();
        }
    }
      
    @Override
    public void onPause(){
    	//setContentView(R.layout.nologin_layout);
    	//Log.d("Login","LOGIN PAUSED");
        super.onPause();
        Log.d("Login", "Destory Login Activity");
        this.finish();
    }
    
    @Override
    public void onResume() {
    	Log.d("Login","LOGIN RESUMED");
        super.onResume();
    }
        
    @Override
    public void onDestroy() {
    	
    	/*
    	if (am!=null && isServerConnected)
    		am.closeServer();
    	isServerConnected=true;
		*/

    	/*DEBUG What needs to be done here, if anything? */
    	
    	Log.d("Login","LOGIN DESTROYED");
        super.onDestroy();
    }
    
    @Override
    public void onStop() {
    	/*DEBUG What needs to be done here, if anything? */
        Log.d("Login","LOGIN STOPPED");
        super.onStop();
    } 

}
